<?php


namespace App\Http\Controllers\Api\v1;


use App\common;
use App\Http\Controllers\Controller;
use App\server\CurlServer;
use App\server\FaceServer;
use Illuminate\Http\Request;

class RecognitionController extends Controller
{
    /**
     * 获取access_token
     */
    public function actionFace(){
        $client_id = 'nkhRGHht9wwyGlGOW4X2L610';
        $client_secret = 'vDLFWkkkWUcrF8kr62oetf5OsLdmUpGe';
        $url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&";
        $curl = new CurlServer();
        $data = $curl->post($url);
        return json_decode($data,true)['access_token'];
    }
    /**
     * 人脸检测
     */
    public function actionRun() {
        $face =new FaceServer();
        $image = 'https://img2.baidu.com/it/u=3354585195,1512541150&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500';
        $data = $face->facedete($image,'URL');
        return $data;
    }
    /**
     *  添加用户组
     */
    public function actionAddGroup(Request $request){
        try {
            $group = $request->post('group_name');
            $face =new FaceServer();
            $res = $face->addGrooup($group);
            return common::success('0',$res,'添加成功');
        }catch (\Exception $exception){
            return common::fail('50001','',$exception->getMessage());
        }
    }
    /**
     *  删除用户组
     */
    public function actionDelGroup(Request $request){
        try {
            $group = $request->post('group_name');
            $face =new FaceServer();
            $res = $face->delGroup($group);
            return common::success('0',$res,'删除成功');
        }catch (\Exception $exception){
            return common::fail('50001','',$exception->getMessage());
        }
    }
    /**
     * 添加人脸
     * @param Request $request
     * @return array|false|string
     */
    public function actionUserAdd()
    {
        try {
            $QniuImg='https://2008a-zfw.oss-cn-shanghai.aliyuncs.com/rules.jpg';
            $face =new FaceServer();
            $res = $face->userAdd($QniuImg,'URL','wjy2008a2','1');
            return common::success(0,$res,'人脸添加成功');
        }catch (\Exception $exception){
            return common::fail(1,'',$exception->getMessage());
        }
    }
    /**
     * 人脸搜索
     */
    public function actionSeachFace(Request $request)
    {
        try {
//            $image = 'https://2008a-zfw.oss-cn-shanghai.aliyuncs.com/rules.jpg';
            $image = 'https://ts2.cn.mm.bing.net/th?id=OIP-C.ekfq34aN4t_4oQ9rNlecRgHaLH&w=204&h=306&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2';
            $face = new FaceServer();
            $res = $face->fachSeach($image,'URL','wjy2008a2','1');
            return common::success(0,$res,'人脸识别成功');
        } catch (\Exception $exception){
            return common::fail(1,'',$exception->getMessage());
        }

    }
}
